package com.learn.xueer.ui.activity

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.View.GONE
import android.view.View.NOT_FOCUSABLE
import android.view.View.VISIBLE
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.learn.xueer.app.base.BaseActivity1
import com.learn.xueer.data.model.bean.WordInfoResponse
import com.learn.xueer.databinding.ActivityReadAfterBinding
import com.learn.xueer.ui.viewmodel.RequestHomeViewModel
import kotlinx.android.synthetic.main.activity_read_after.currentProgress
import kotlinx.android.synthetic.main.activity_read_after.nextButton
import kotlinx.android.synthetic.main.activity_read_after.progressSeek
import kotlinx.android.synthetic.main.activity_read_after.totalProgress
import kotlinx.android.synthetic.main.activity_read_after.tvProgress
import kotlinx.android.synthetic.main.activity_read_after.tvTitle
import kotlinx.android.synthetic.main.activity_unit_words.answerTypeView

class ReadAfterActivity : BaseActivity1<RequestHomeViewModel, ActivityReadAfterBinding>() {

    private lateinit var wordInfos: ArrayList<WordInfoResponse>
    private var currentPosition = 0

    @RequiresApi(Build.VERSION_CODES.O)
    override fun initView(savedInstanceState: Bundle?) {

        mViewBind.viewmodel = mViewModel
        mViewBind.click = ProxyClick()

        tvTitle.text = intent.getStringExtra("title")
        wordInfos = Gson().fromJson(
            intent.getStringExtra("wordInfo"),
            object : TypeToken<ArrayList<WordInfoResponse>>() {}.type
        )
        totalProgress.text = "/${wordInfos.size}"
        progressSeek.isEnabled = false
        progressSeek.isClickable = false
        progressSeek.focusable = NOT_FOCUSABLE
        progressSeek.max = wordInfos.size

        bindUiData()
    }

    private fun bindUiData() {
        currentProgress.text = "${currentPosition+1}"
        progressSeek.progress = currentPosition+1
        mViewModel.word.set(wordInfos[currentPosition].words)
        mViewModel.wordImage.set(wordInfos[currentPosition].wordPic)
        mViewModel.symbol.set("[${wordInfos[currentPosition].symbol}]")
        mViewModel.sentences.set(wordInfos[currentPosition].sentences)
        mViewModel.pretations.set(wordInfos[currentPosition].pretations)
    }

    inner class ProxyClick {
        fun returnBack() {
            finish()
        }

        fun last() {
            if (currentPosition == 0) {
                Toast.makeText(this@ReadAfterActivity, "已经是第一个了", Toast.LENGTH_SHORT).show()
            } else {
                currentPosition -= 1
                if (currentPosition < wordInfos.size - 1) {
                    nextButton.visibility = VISIBLE
                }
                bindUiData()
            }
        }

        fun next() {
            if (currentPosition < wordInfos.size - 1) {
                currentPosition += 1
                bindUiData()
                if (currentPosition == wordInfos.size - 1) {
                    nextButton.visibility = GONE
                }
            }
        }
    }
}